package org.bluedream.core.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.module.sys.entity.Organization;
import org.bluedream.core.module.sys.entity.org.OrgRelation;
import org.bluedream.core.module.sys.service.OrganizationService;
import org.bluedream.core.utils.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("${adminPath}/organization")
public class OrganizationController extends BaseController {
    @Resource
    private OrganizationService organizationService;

    @ModelAttribute
    public Organization get(String id){
        return organizationService.getById(id);
    }

    @RequiresPermissions("org:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Organization organization, Model model){
        model.addAttribute("isSystem" , UserUtil.isSystem());
        model.addAttribute("organization" , organization);
        return "sys/organization/organizationList";
    }

    @RequiresPermissions("org:view")
    @RequestMapping("/form")
    public String form(Organization organization, Model model){
        organization = organizationService.createNextNode(organization);
        model.addAttribute("organization" , organization);
        return "sys/organization/organizationForm";
    }

    @RequiresPermissions("org:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Organization> findPage(Organization organization){
        return organizationService.findPage(organization);
    }

    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map<String , Object>> treeData(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return organizationService.treeData(excludeCode , isShowCode);
    }

/*    @RequestMapping("/treeData4Login")
    @ResponseBody
    public List<Map<String , Object>> treeData4Login(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return organizationService.treeData(excludeCode , isShowCode , (List<Organization>) null);
    }*/

    @RequiresPermissions("org:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(Organization organization){
        boolean flag = organizationService.save(organization);
        return "组织信息保存成功";
    }

    /**
     * 创建下一节点
     * @param organization
     * @return
     */
    @RequiresPermissions("org:edit")
    @RequestMapping(value = "/createNextNode" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Organization createNextNode(@RequestBody Organization organization){
        return organizationService.createNextNode(organization);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Organization organization){
        try {
            boolean flag = organizationService.delete(organization);
            if (flag){
                return "组织信息删除成功";
            }
            return "组织信息删除失败";
        }catch (Exception e1){
            return e1.getMessage();
        }
    }

    @RequestMapping("/getOrganization")
    @ResponseBody
    public Map<String , String> getOrganization(OrgRelation orgRelation){
        Organization org = organizationService.getOrganization(orgRelation);
        Map<String , String> orgMap = new HashedMap();
        orgMap.put("id" , org.getId());
        orgMap.put("name" , "[" + org.getTreeCode() + "]" + org.getTreeName());
        return orgMap;
    }
}
